package com.freight.file.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.redis.RedisTemplateHelper;
import com.freight.core.common.utils.HttpUtils;
import com.freight.core.common.utils.ObjectUtil;
import com.freight.file.constants.FileConstant;
import com.freight.file.vo.PicFileVo;
import com.freight.file.vo.VehcileBackPicVo;
import com.freight.file.vo.VehcileFacePicVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

import static com.freight.file.util.FileUtils.baiud_img_base64;
import static com.freight.file.util.BaiDuToken.getAuth;
public class VehicleOcrUtil {
    /*
     * 获取参数的json对象
     */
    public static JSONObject getParam(int type, String dataValue) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("dataType", type);
            obj.put("dataValue", dataValue);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }




    //百度地址
    public static String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license";

    public static PicFileVo baiDuJustVehicle(String imgBase64, RedisTemplateHelper redisTemplate) {

        HashMap<String, Object> result = new HashMap<>();
        VehcileFacePicVo resO = null;
        try {
            String param = "vehicle_license_side=" + "front" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject vehiclejson = res_obj.getJSONObject("words_result");

            result.put("vehicleType", vehiclejson.getJSONObject("车辆类型").getString("words"));
            result.put("vehicleNum", vehiclejson.getJSONObject("号牌号码").getString("words"));
            result.put("vehicleOwner", vehiclejson.getJSONObject("所有人").getString("words"));
            result.put("useCharacter", vehiclejson.getJSONObject("使用性质").getString("words"));
            result.put("address", vehiclejson.getJSONObject("住址").getString("words"));
            result.put("engineNum", vehiclejson.getJSONObject("发动机号码").getString("words"));
            result.put("model", vehiclejson.getJSONObject("品牌型号").getString("words"));
            result.put("registerDate", vehiclejson.getJSONObject("注册日期").getString("words"));
            result.put("issueDate", vehiclejson.getJSONObject("发证日期").getString("words"));
            result.put("vin", vehiclejson.getJSONObject("车辆识别代号").getString("words"));
            result.put("issuingAuthority", vehiclejson.getJSONObject("发证单位").getString("words"));
            resO = (VehcileFacePicVo) ObjectUtil.autoFillMapFields(result, VehcileFacePicVo.class);

            System.out.println(result);
            return resO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static PicFileVo baiDuBackVehicle(String imgBase64, RedisTemplateHelper redisTemplate) {
        // 请求url
        HashMap<String, Object> result = new HashMap<>();
        VehcileBackPicVo resO = null;

        try {
            String param = "vehicle_license_side=" + "back" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject vehiclejson = res_obj.getJSONObject("words_result");

            result.put("approvedCapacity", vehiclejson.getJSONObject("核定载人数").getString("words"));
            result.put("approvedLoad", vehiclejson.getJSONObject("核定载质量").getString("words"));
            result.put("fileNo", vehiclejson.getJSONObject("档案编号").getString("words"));
            result.put("grossMass", vehiclejson.getJSONObject("总质量").getString("words"));
            result.put("tractionMass", vehiclejson.getJSONObject("准牵引总质量").getString("words"));
            result.put("unladenMass", vehiclejson.getJSONObject("整备质量").getString("words"));
            result.put("inspectionRecord", vehiclejson.getJSONObject("检验记录").getString("words"));
            result.put("overallDimension", vehiclejson.getJSONObject("外廓尺寸").getString("words"));
            result.put("oilType", vehiclejson.getJSONObject("燃油类型").getString("words"));

            resO = (VehcileBackPicVo) ObjectUtil.autoFillMapFields(result, VehcileBackPicVo.class);
            System.out.println(result);
            return resO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resO;
    }


}
